package org.itworks.biodev.gui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import org.itworks.biodev.gui.*;
import org.itworks.biodev.model.Customer;
import org.itworks.helpers.MVCNotification;
import org.itworks.helpers.gui.ButtonColumn;

public class SalariesPanelView extends JPanel implements Observer{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7684225512226515643L;
	public static final int FILL_SALARY = 1;
	private static SalariesPanelView salariesPanelView = null;
	private MainWindowView parent = null;
	private SalariesPanelControler controler = null;
	
	private JButton filterSalariesButton;
    private JLabel fromSalariesLabel;
    private com.toedter.calendar.JDateChooser fromSalariesDateChooser;
    private JScrollPane salariesListScrollPane;
    private JTable salariesListTable;
    private JLabel salariesTitleLabel;
    private JLabel toSalariesLabel;
    private com.toedter.calendar.JDateChooser toSalariesDateChooser;
    
    private SalariesPanelView(MainWindowView parent, SalariesPanelControler controler) {
    	this.parent = parent;
    	this.controler = controler;
    	this.controler.getModel().addObserver(this);
    	
    	initComponents();
    	initHandlers();
    	
    	this.controler.viewLoaded();
    }
    
    private void initComponents() {
    	salariesTitleLabel = new javax.swing.JLabel();
        fromSalariesLabel = new javax.swing.JLabel();
        fromSalariesDateChooser = new com.toedter.calendar.JDateChooser();
        toSalariesLabel = new javax.swing.JLabel();
        toSalariesDateChooser = new com.toedter.calendar.JDateChooser();
        filterSalariesButton = new javax.swing.JButton();
        salariesListScrollPane = new javax.swing.JScrollPane();
        salariesListTable = new javax.swing.JTable();

        salariesTitleLabel.setFont(new java.awt.Font("Trebuchet MS", 1, 18));
        salariesTitleLabel.setText("Salaires");

        fromSalariesLabel.setText("Du :");

        toSalariesLabel.setText("Au :");

        filterSalariesButton.setText("Filtrer");

        salariesListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Du", "Au", "Total"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        salariesListScrollPane.setViewportView(salariesListTable);

        javax.swing.GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(salariesListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 1080, Short.MAX_VALUE)
                    .addComponent(salariesTitleLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(fromSalariesLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(fromSalariesDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(toSalariesLabel)
                        .addGap(18, 18, 18)
                        .addComponent(toSalariesDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(filterSalariesButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(salariesTitleLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fromSalariesLabel)
                    .addComponent(fromSalariesDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(toSalariesLabel)
                    .addComponent(toSalariesDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(filterSalariesButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(salariesListScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(559, Short.MAX_VALUE))
        );
    }
    
    private void initHandlers() {
    	this.filterSalariesButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				// TODO Auto-generated method stub
				try {
					controler.filterSalariesButtonActionPerformed(actionEvent,fromSalariesDateChooser,toSalariesDateChooser);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		});    	
    }
    
    public static SalariesPanelView getInstance(MainWindowView parent) {
    	if(salariesPanelView == null) {
    		salariesPanelView = new SalariesPanelView(parent, new SalariesPanelControler(new SalariesPanelModel()));
    	}
    	
    	return salariesPanelView;
    }

	@Override
	public void update(Observable arg0, Object arg) {
            // TODO Auto-generated method stub
            MVCNotification notification = (MVCNotification)arg;
            int reason = notification.getReason();
        
            switch(reason) {
            case FILL_SALARY:
            	
                // todo.
                try {
                    fillSalaryTable( (Float)notification.getObject() );
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
	}

	private void fillSalaryTable(Float object) {
		// TODO Auto-generated method stub
   	       
			DefaultTableModel model = new javax.swing.table.DefaultTableModel(
		            new Object [][] {

		            },
		            new String [] {
		            	"Du", "Au", "Total"
		            }
		        ) {
					               
					private static final long serialVersionUID = -7580284705472131221L;
					
					Class[] types = new Class [] {
			                java.lang.String.class, java.lang.String.class, java.lang.Float.class
			            };

			            public Class getColumnClass(int columnIndex) {
			                return types [columnIndex];
			            }
			            
					boolean[] canEdit = new boolean [] {
		                false, false, false
		            };

		            public boolean isCellEditable(int rowIndex, int columnIndex) {
		                return canEdit [columnIndex];
		            }
			        
				};
				
				salariesListTable.setModel(model);	       
	       
           Vector<Object> rowData = new Vector<Object>();
           rowData.add(this.fromSalariesDateChooser.getDate().toLocaleString());
           rowData.add(this.toSalariesDateChooser.getDate().toLocaleString());
           rowData.add(object);
           
           model.addRow(rowData);
	}
}
